package com.podhradie.net.themperature.client;

import it.m2.net.telnet.NVTCom;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

import com.podhradie.net.themperature.server.UdpServer;
import com.podhradie.net.themperature.tools.Context;
import com.podhradie.net.themperature.tools.Context.LogCategory;

public class Client {

	String ipAddress;
	int port;
	boolean reading;

	public Client(String ipAddress, int port) {
		this.ipAddress = ipAddress;
		this.port = port;
		reading = true;
		Thread t1 = new Thread(thReader);
		t1.start();
	}

	public void stop() {
		reading = false;
	}

	Runnable thReader = new Runnable() {

		@Override
		public void run() {
			NVTCom nvtCom = connect(ipAddress, port);

			if (nvtCom != null) {
				
				Context.get().printLine(LogCategory.CLIENT,"Start read");
				
			//	new UdpServer();
				while (reading) {
					byte buff[] = new byte[8];

					try {
						nvtCom.read(buff);
						Context.get().setConnected(true);
					} catch (IOException e) {
						Context.get().printLine(LogCategory.CLIENT,"CLient " +ipAddress+":"+port+" not responding");
						break;
					}

					String value = new String(buff);
					double temperatureDouble = parseTemperature(value);
					
					if (temperatureDouble!= -1000){
						Context.get().setTemperature(temperatureDouble);
						SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy  HH:mm:ss");
						String dateAsString = simpleDateFormat.format(new Date());
						Context.get().setTimeStamp(dateAsString);
					}
					
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				try {
					nvtCom.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Context.get().setConnected(false);
				Context.get().printLine(LogCategory.CLIENT,"Connection to " +ipAddress+":"+port+" has been closed");

			}

		}
	};
	
	private double parseTemperature (String line){
		double result = -1000;
		if (line != null && line.length() == 8 && (line.charAt(7)=='\r') && (line.charAt(6)=='C') && ((line.charAt(0)=='+') ||(line.charAt(0)=='-'))){
			try {
				result = Double.parseDouble(line.substring(0, 6));	
			}catch (Exception  e) {
				// TODO: handle exception
				Context.get().printLine(LogCategory.CLIENT, "ERROR parse temperature problem, value: "+line);
			}
			
		}
		return result;
	}
	
	private NVTCom connect (String ipAddress, int port){
		
		Context.get().printLine(LogCategory.CLIENT,"Connecting  to " + ipAddress + ":" + port);
		NVTCom nvtCom = null;
		try {
			nvtCom = new NVTCom(ipAddress, port);
		} catch (IOException e) {
			Context.get().printLine(LogCategory.CLIENT,"Connection problem");
		}

		if (nvtCom != null) {
			Context.get().printLine(LogCategory.CLIENT,"Connected");
			
			//flush buffer
			try {
				nvtCom.read(new byte[2048]);
				Context.get().printLine(LogCategory.CLIENT,"Flush buffer");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return nvtCom;
	}
}
